using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Globals;
using Presentation;
using Presentation.Contracts;

namespace ProjektZespolowy.Messages
{
    public partial class CVMessage : System.Web.UI.Page,ICVMessageView
    {
        #region Members

        /// <summary>
        /// Instance of presenter.
        /// </summary>
        private CVMessagePresenter presenter;

        #endregion Members

        #region Override Methods

        /// <summary>
        /// Raises the <see cref="E:System.Web.UI.Control.Init"></see> event.
        /// </summary>
        /// <param name="e">An <see cref="T:System.EventArgs"></see> object that contains the event data.</param>
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            presenter = new CVMessagePresenter(this);
        }

        #endregion Override Methods

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request["UserID"]))
            {
                lHeader.Text = "Utworz nowa wiadomosc";
                lFtomTo.Text = "Do:";
                lLogin.Text = presenter.GetLogin(int.Parse(UrlID()));
                bSend.Text="Wyslij";
            }
            else if (!string.IsNullOrEmpty(Request["MsgID"]))
            {
                lHeader.Text = "Odebrane wiadomosci";
                lFtomTo.Text = "Od:";
                lLogin.Text = presenter.GetLogin(presenter.GetUserIdOfSender(int.Parse(UrlID())));
                TextBox1.Text = presenter.GetTopicOfMessage(int.Parse(UrlID()));
                TextBox2.Text = presenter.GetContentOfMessage(int.Parse(UrlID()));
                TextBox1.ReadOnly = true;
                TextBox2.ReadOnly = true;
                bSend.Text = "Odpowiedz";
            }

        }



        protected void bSend_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request["UserID"]))

                presenter.CreateMessage(TextBox1.Text, TextBox2.Text);

            else

                Response.Redirect("/Mail/CVMessage.aspx?UserID="+presenter.GetUserIdOfSender(int.Parse(UrlID())));

        }

        protected void bClose_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request["UserID"]))
                Response.Redirect("/Friends/Friends.aspx");
            else 
                Response.Redirect("/Mail/Inbox.aspx");
            
        }

        #region ICVMessageView Members

        public string SessionID(){

            return Session[ContentNamesGlobals.SessionWithUserId].ToString();
        }

        public string UrlID()
        {
            if(!string.IsNullOrEmpty(Request["UserID"]))
                return Request.QueryString["UserID"];
            else 
                return Request.QueryString["MsgID"];
        }

        #endregion
    }
}
